package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApCollectionBehaviorMapper;
import com.heima.article.service.ApCollectionBehaviorService;

import com.heima.common.exception.CustomException;
import com.heima.feigns.behavior.BehaviorFeign;
import com.heima.model.article.dtos.CollectionBehaviorDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.article.pojos.ApCollection;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service
public class ApCollectionBehaviorServiceImpl extends ServiceImpl<ApCollectionBehaviorMapper, ApCollection> implements ApCollectionBehaviorService {
     @Autowired
     BehaviorFeign behaviorFeign;

    @Override
    public ResponseResult collection(CollectionBehaviorDto dto) {
        //检查参数
        if (dto == null || dto.getOperation() == null || dto.getEntryId() == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        //获取当前登录用户
        ApUser user = AppThreadLocalUtils.getUser();
        if (user.getId() == null && dto.getEquipmentId() != 0) {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApBehaviorEntry apBehaviorEntry = behaviorFeign.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
        if (apBehaviorEntry == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        //查询收藏行为
        ApCollection apCollection = getOne(Wrappers.<ApCollection>lambdaQuery()
                .eq(ApCollection::getArticleId, dto.getEntryId())
                .eq(ApCollection::getEntryId,apBehaviorEntry.getId()));
        //判断文章是否收藏
        if (apCollection != null && apCollection.isCollectionArticle()) {
            throw new CustomException(AppHttpCodeEnum.DATA_EXIST, "您已经收藏该文章");
        }
        //判断参数保存收藏行为或取消收藏行为
        if (apCollection == null && dto.getOperation().equals((short) 0)) {
            apCollection = new ApCollection();
            apCollection.setArticleId(dto.getEntryId());
            apCollection.setCollectionTime(new Date());
            apCollection.setEntryId(apBehaviorEntry.getId());
            apCollection.setPublishedTime(new Date());
            apCollection.setType(dto.getType());
            save(apCollection);
        } else {
           remove(Wrappers.<ApCollection>lambdaQuery()
                    .eq(ApCollection::getArticleId, dto.getEntryId())
                    .eq(ApCollection::getEntryId, apBehaviorEntry.getId()));
        }
        //返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
